package com.paas.form.core.element.control.impl.searchItem;

import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.core.element.base.Utilmethod;
import com.paas.form.core.element.control.FormItem;
import com.paas.form.core.element.field.FieLdsModel;
import com.paas.form.core.element.jsContent.ScriptContent;
import lombok.Data;


@Data
public class ElCascaderItem implements FormItem {
    private FieLdsModel field;
    private String htmlType;
    public static final String PrOP_JS_DATA = "                ${field.vModel}attrProps : JSON.parse('${field.props.props}'), \n";
    public static final String PrOP_JS_DATA_NULL = "                ${field.vModel}attrProps : [], \n";
    public static final String EL_CASCADER_JS_DATA = "                ${field.vModel}Options :  " +
            "[\n #foreach($item in ${field.schemeSlot.options})\n" +
            "#set($maohao=':')\n" +
            "                  {" +
            "#if(${item.id})\n" +
            "id${maohao}'${item.id}'," +
            "#end\n" +
            "#if(${item.fullName})\n" +
            "fullName${maohao}'${item.fullName}'," +
            "#end\n" +
            "#if(${item.parentId})\n" +
            "parentId${maohao}'${item.parentId}'," +
            "#end\n" +
            "#if(${item.hasChildren})\n" +
            "hasChildren${maohao}'${item.hasChildren}'," +
            "#end\n" +
            "#if(${item.children})\n" +
            "children${maohao}'${item.children}'," +
            "#end\n" +
            "#if(${item.children})\n" +
            "children${maohao}'${item.children}'," +
            "#end\n" +
            "#if(${item.enCode})\n" +
            "enCode${maohao}'${item.enCode}'," +
            "#end\n" +
            "},\n" +
            "#end " +
            "               ],\n";

    public static final String EL_CASCADER_HTML ="<el-cascader v-model=\"${formObject}.${field.vModel}\" :options=\"${field.vModel}attrOptions\" clearable\n" +
            "                :show-all-levels=\"${field.showalllevels}\" style ='width:100%' :props=\"${field.vModel}attrProps\" filterable\n" +
            "                placeholder=\"请选择${field.config.label}\" ></el-cascader>";


    public String formMakeVue(){
        String elementStart = "<" +this.field.getConfig().getTag() + " v-model=\"${formObject}.${field.vModel}\" ";
        String attrsStr = Utilmethod.attriAdd(this.field);
        String elementEnd = ":options=\"${field.vModel}attrOptions\" :props=\"${field.vModel}attrProps\"   ></" + this.field.getConfig().getTag() +">\n";
        String element= elementStart +attrsStr +elementEnd;
        return element;
    }
    public String MakeJsData(){
        String dataStr = "";
        if(BeanUtil.isNotEmpty(this.field) && StringUtil.isNotEmpty(this.field.getOptions())){
            dataStr= dataStr +"                ${field.vModel}attrOptions : JSON.parse('${field.options}'),\n";
        }else {
            dataStr= dataStr +"                ${field.vModel}attrOptions : [], \n";
        }
        if(BeanUtil.isNotEmpty(this.field) && BeanUtil.isNotEmpty(this.field.getProps())){
            dataStr= dataStr +"                ${field.vModel}attrProps : JSON.parse('${field.props.props}'),\n ";
        }else {
            dataStr= dataStr +"                ${field.vModel}attrProps : [],\n";
        }
        return dataStr;
    }

    public ElCascaderItem(FieLdsModel field,String htmlType){
        this.field = field;
        this.htmlType = htmlType;

    }


    @Override
    public String makeVue() {
        if(this.htmlType.equals("form")){
            return this.formMakeVue();
        }else {
            return EL_CASCADER_HTML;
        }
    }

    @Override
    public ScriptContent makeJs() {
        ScriptContent scriptContent = new ScriptContent();
        String jsdatStr = "";
        jsdatStr =jsdatStr +EL_CASCADER_JS_DATA;
        jsdatStr = jsdatStr +this.MakeJsData();
//        if(BeanUtil.isNotEmpty(this.field) && StringUtil.isNotEmpty(this.field.getProps())){
//            jsdatStr = jsdatStr +PrOP_JS_DATA;
//        }else {
//            jsdatStr = jsdatStr +PrOP_JS_DATA_NULL;
//        }
        scriptContent.setData(jsdatStr);
        return scriptContent;
    }
}
